<?php
/**
 * Class that operate on table 'sales'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2013-06-26 02:15
 */
class SalesMySqlDAO implements SalesDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return SalesMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM sales WHERE PKSales = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM sales';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM sales ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param sale primary key
 	 */
	public function delete($PKSales){
		$sql = 'DELETE FROM sales WHERE PKSales = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($PKSales);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param SalesMySql sale
 	 */
	public function insert($sale){
		$sql = 'INSERT INTO sales (SalesDate, Descunt, TotalAmount, Active, Visible) VALUES (?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($sale->salesDate);
		$sqlQuery->set($sale->descunt);
		$sqlQuery->set($sale->totalAmount);
		$sqlQuery->setNumber($sale->active);
		$sqlQuery->setNumber($sale->visible);

		$id = $this->executeInsert($sqlQuery);	
		$sale->pKSales = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param SalesMySql sale
 	 */
	public function update($sale){
		$sql = 'UPDATE sales SET SalesDate = ?, Descunt = ?, TotalAmount = ?, Active = ?, Visible = ? WHERE PKSales = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($sale->salesDate);
		$sqlQuery->set($sale->descunt);
		$sqlQuery->set($sale->totalAmount);
		$sqlQuery->setNumber($sale->active);
		$sqlQuery->setNumber($sale->visible);

		$sqlQuery->setNumber($sale->pKSales);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM sales';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryBySalesDate($value){
		$sql = 'SELECT * FROM sales WHERE SalesDate = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDescunt($value){
		$sql = 'SELECT * FROM sales WHERE Descunt = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByTotalAmount($value){
		$sql = 'SELECT * FROM sales WHERE TotalAmount = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByActive($value){
		$sql = 'SELECT * FROM sales WHERE Active = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByVisible($value){
		$sql = 'SELECT * FROM sales WHERE Visible = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteBySalesDate($value){
		$sql = 'DELETE FROM sales WHERE SalesDate = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDescunt($value){
		$sql = 'DELETE FROM sales WHERE Descunt = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByTotalAmount($value){
		$sql = 'DELETE FROM sales WHERE TotalAmount = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByActive($value){
		$sql = 'DELETE FROM sales WHERE Active = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByVisible($value){
		$sql = 'DELETE FROM sales WHERE Visible = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return SalesMySql 
	 */
	protected function readRow($row){
		$sale = new Sale();
		
		$sale->pKSales = $row['PKSales'];
		$sale->salesDate = $row['SalesDate'];
		$sale->descunt = $row['Descunt'];
		$sale->totalAmount = $row['TotalAmount'];
		$sale->active = $row['Active'];
		$sale->visible = $row['Visible'];

		return $sale;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return SalesMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>